/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.SpecefcationDAO;
import com.core.database.Manager;
import com.core.entities.Specefcation;
import com.core.exception.DAOException;

/**
 * 
 * @author himanasser
 */
public class SpecefcationDAOImpl implements SpecefcationDAO {
	Specefcation specefcation = new Specefcation();
	ArrayList<Specefcation> specefcationsList = new ArrayList<Specefcation>();


	Connection connection = null;
	private static SpecefcationDAOImpl instance=null;
	public static SpecefcationDAOImpl getInstance(){
		if(instance==null){
			instance=new SpecefcationDAOImpl();
		}
		return instance;
	}

	@Override
	public Specefcation findByName(String name) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM specefcation WHERE name = '" + name
					+ "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					specefcation.setId(rs.getInt(1));
					specefcation.setName(rs.getString(2));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return specefcation;
	}

	@Override
	public void insert(Specefcation o) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "INSERT INTO specefication (name) VALUE (?)";
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, o.getName());

			int result = ps.executeUpdate();
			if (result != 1) {
				throw new SQLException("no row inserted!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("Cannot add new Specefcation", ex);
		} finally {
			Manager.closeConnection(connection);
		}
	}

	@Override
	public Specefcation findById(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = "SELECT * FROM specefcation WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
				if (count == 1) {
					specefcation.setId(rs.getInt(1));
					specefcation.setName(rs.getString(2));

				}
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
		return specefcation;
	}

	@Override
	public List<Specefcation> findAll() throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " SELECT * FROM specefcation ";
			PreparedStatement ps = connection.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				specefcation.setId(rs.getInt(1));
				specefcation.setName(rs.getString(2));

				specefcationsList.add(specefcation);

			}
		} catch (SQLException s) {
			s.printStackTrace();
		}
		return specefcationsList;
	}

	@Override
	public void delete(int id) throws DAOException {
		try {
			connection = Manager.getConnection();
			String sql = " DELETE FROM specefcation WHERE id = '" + id + "'";
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate();
			if (result != 1) {
				ps.setString(1, specefcation.getName());
			}

		} catch (SQLException s) {
			s.printStackTrace();

		}
	}

	@Override
	public void update(Specefcation o) throws DAOException {

		try {
			connection = Manager.getConnection();
			String sql = "UPDATE specefcation SET name = '" + o.getName()
					+ "' WHERE id = " + o.getId();
			PreparedStatement ps = connection.prepareStatement(sql);
			int result = ps.executeUpdate(sql);

			if (result != 1) {
				ps.setString(1, o.getName());
			}

		} catch (SQLException s) {
			s.printStackTrace();
		}
	}
}
